package com.example.servicetest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;

public class DuffModeView extends FrameLayout {

   private Paint paint;
   private int width, height;
   private Bitmap srcBm, dstBm;

   private PorterDuffXfermode duffXfermode = new PorterDuffXfermode(PorterDuff.Mode.ADD);

   public DuffModeView(Context context) {
      this(context, null);
   }

   public DuffModeView(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, 0);
   }

   public DuffModeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      init();
   }

   private void init() {
      //禁用硬件加速
      setLayerType(View.LAYER_TYPE_SOFTWARE, null);
      //初始化画笔
      paint = new Paint();
   }

   @Override
   protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
      super.onLayout(changed, left, top, right, bottom);
      width = right - left;
      height = bottom - top;
      srcBm = createSrcBitmap(width, height);
      dstBm = createDstBitmap(width, height);
   }

   @Override
   protected void dispatchDraw(Canvas canvas) {
      //离屏绘制
      int layerID = canvas.saveLayer(0, 0, getWidth(), getHeight(), paint, Canvas.ALL_SAVE_FLAG);
      canvas.drawBitmap(dstBm, 0, 0, paint);

      super.dispatchDraw(canvas);

      paint.setXfermode(duffXfermode);
      canvas.drawBitmap(srcBm, 0, 0, paint);
      paint.setXfermode(null);
      canvas.restoreToCount(layerID);
   }


   public Bitmap createDstBitmap(int width, int height) {
      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      Paint dstPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      dstPaint.setColor(Color.parseColor("#00b7ee"));
      canvas.drawCircle(width / 3, height / 3, width / 3, dstPaint);
      return bitmap;
   }

   public Bitmap createSrcBitmap(int width, int height) {
      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      Paint scrPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      scrPaint.setColor(Color.parseColor("#ec6941"));
      canvas.drawRect(new Rect(width / 3, height / 3, width, height), scrPaint);
      return bitmap;
   }
}